package com.yupi.usapiinterface.controller;

import com.yupi.usapiclientsdk.model.User;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 名称api
 *
 * @author zys
 */
@RestController
@RequestMapping("/name")
public class UserController {
    /**
     * get请求获取姓名
     * @param name
     * @return
     */
    @GetMapping("/get")
    public String getNameByGet(String name){
        return "my name is " + name;
    }

    /**
     * post请求获取姓名
     * @param name
     * @return
     */
    @PostMapping("/post")
    public String getNameByPost(@RequestParam String name){
        return "my name is " + name;
    }

    /**
     * 请求参数为对象时获取姓名
     * @param user
     * @return
     */
    @PostMapping("/user")
    public String getUserNameByPost(@RequestBody User user, HttpServletRequest request){
        String accessKey = request.getHeader("accessKey");
        String nonce = request.getHeader("nonce");
        String timestamp = request.getHeader("timestamp");
        String sign = request.getHeader("sign");
        String body = request.getHeader("body");
        // todo 实际情况应该是去数据库中查
//        if (!accessKey.equals("zys")) {
//            throw new RuntimeException("无权限");
//        }
//        if (Long.parseLong(nonce) > 10000) {
//            throw new RuntimeException("无权限");
//        }
//        String serverSign = SignUtils.genSign(body, "abc");
//        if (!sign.equals(serverSign)) {
//            throw new RuntimeException("无权限");
//        }
        String res = "my name is " + user.getName();
        return res;
    }
}
